package com.yyfax.common.form;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.NumberKeyListener;
import android.util.AttributeSet;
import android.view.*;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.yyfax.common.R;
import com.yyfax.common.toast.CompatToast;
import com.yyfax.common.tools.IDCardValidate;
import com.yyfax.common.tools.Toaster;

/**
 * Created by fuqiang on 2017/5/4.
 * 带右边按钮的输入框
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class FormInputView extends LinearLayout implements View.OnClickListener {

	private boolean canScan = true;

	/**
	 * 点击右边图标回调
	 */
	private OnDrawableRightClickListener mOnClickDrawableRightListener;

	/**
	 * 是否检查身份证的年龄(绑定银行卡页面不检查, 推荐贷款页面检查, 默认false, 不检查)
	 */
	private boolean mCheckAge;

	public ImageButton mIdCardIb;

	public ClearEditText mREditText;

	private OnFocusChangeListener mListener;


	public FormInputView(Context context) {
		super(context);
	}

	public FormInputView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public FormInputView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context, attrs);
	}

	void init(Context context, AttributeSet attrs) {
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.FormRow);
		mCheckAge = ta.getBoolean(R.styleable.FormRow_checkAge, false);
		setOrientation(LinearLayout.HORIZONTAL);
		setGravity(Gravity.CENTER_VERTICAL);
		setPadding(0, getPaddingTop(), getPaddingRight(), getPaddingBottom());
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.id_card_layout, this);
		mREditText = (ClearEditText) view.findViewById(R.id.id_card_edit_text);
		mREditText.setEllipsize(TextUtils.TruncateAt.END);
		int mType = ta.getInt(R.styleable.FormRow_customType, 3);
		mIdCardIb = (ImageButton) view.findViewById(R.id.id_card_ib);
		switch (mType) {
			case FormType.TYPE_ID_CARD://身份证
				mREditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(18)});
				mREditText.setKeyListener(new NumberKeyListener() {
					@Override
					protected char[] getAcceptedChars() {
						return new char[]{'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'x', 'X'};
					}

					@Override
					public int getInputType() {
						return InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
					}
				});
				break;
			case FormType.TYPE_BANKCARD://银行卡
				mREditText.setInputType(InputType.TYPE_CLASS_NUMBER);
				mREditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(19)});
				break;
			case FormType.TYPE_NORMAL_WITH_RIGHT_BTN://带右边按钮的输入框:
				int maxlength = ta.getInt(R.styleable.FormRow_maxLength, -1);
				if (maxlength >= 0) {
					mREditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxlength)});
				} else {
					mREditText.setFilters(new InputFilter[0]);
				}
				int inputType = ta.getInt(R.styleable.FormRow_android_inputType, EditorInfo.TYPE_NULL);
				if (inputType != EditorInfo.TYPE_NULL)
					mREditText.setInputType(inputType);//设置inputType
				Drawable drawableRight = ta.getDrawable(R.styleable.FormRow_drawableRight);
				if (drawableRight != null)
					mIdCardIb.setImageDrawable(drawableRight);
				break;
		}
		mIdCardIb.setOnClickListener(this);
		int type = ta.getInt(R.styleable.FormRow_customType, 7);
		if (type == 7) {//扫描身份证,目前在CMA用到
			mREditText.setOnFocusChangeListener(new ClearEditText.OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					String applicantLicense = mREditText.getText().toString();
					if (hasFocus) {//如果有焦点
						mIdCardIb.setImageDrawable(getContext().getResources().getDrawable(R.drawable.scan_ic));
						canScan = true;
					} else {//如果没有焦点
						mREditText.setCompoundDrawables(null, null, null, null);//不显示清空按钮
						if (!TextUtils.isEmpty(applicantLicense)) {//如果有内容
							if (!TextUtils.isEmpty(IDCardValidate.checkIdCard(applicantLicense, mCheckAge, getContext().getString(R.string.error_age_strs)))) {//如果身份证不合法
								mIdCardIb.setImageDrawable(getContext().getResources().getDrawable(R.drawable.input_error_flag_icon));//感叹号图标
								CompatToast.showShortToast(getContext().getApplicationContext(), R.string.invalidate_id_card_please_check);
								canScan = false;
							} else {//如果身份证合法
								mIdCardIb.setImageDrawable(getContext().getResources().getDrawable(R.drawable.scan_ic));//扫描图标
								canScan = true;
							}
						} else {//如果没有内容
							mIdCardIb.setImageDrawable(getContext().getResources().getDrawable(R.drawable.scan_ic)); //扫描图标
							canScan = true;
						}
					}
				}
			});
		} else {
			mREditText.setOnFocusChangeListener(new ClearEditText.OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					if (mListener != null)
						mListener.onFocusChange(v, hasFocus);
				}
			});
		}
	}

	@Override
	public void onClick(View v) {
		if (canScan && mOnClickDrawableRightListener != null)
			mOnClickDrawableRightListener.onDrawableRightClick(this);//扫描回调
	}

	public void setOnDrawableRightClickListener(OnDrawableRightClickListener listener) {
		mOnClickDrawableRightListener = listener;
	}

	/**
	 * 点击扫描按钮回调
	 */
	public interface OnDrawableRightClickListener {
		void onDrawableRightClick(View view);
	}


	public void setText(CharSequence text) {
		mREditText.setText(text);
	}

	public String getFormText() {
		return mREditText.getText().toString().trim();
	}

	public void clear() {
		mREditText.getText().clear();
	}

	public boolean ismCheckAge() {
		return mCheckAge;
	}

	public void addTextChangedListener(TextWatcher watcher) {
		mREditText.addTextChangedListener(watcher);
	}

	public interface OnFocusChangeListener {
		void onFocusChange(View v, boolean hasFocus);
	}

	public void setOnFocusChangeListener(OnFocusChangeListener listener) {
		this.mListener = listener;
	}
}
